<template>
<!-- 树形菜单 -->
  <div class="app-container">
    <el-input v-model="filterText" placeholder="Filter keyword" style="margin-bottom:30px;" />

    <el-tree
      ref="tree2"
      :data="data2"
      :props="defaultProps"
      :filter-node-method="filterNode"
      class="filter-tree"
      default-expand-all
    />

  </div>
</template>

<script>
import subject from '@/api/edu/subject'

export default {

  data() {
    return {
      filterText: '',
      data2: [],   //数据列表
      defaultProps: {
        children: 'children',
        label: 'title'   //与数据库返回来的数据字段对应
      }
    }
  },
  watch: {    //  响应数据的变化
    filterText(val) {
      this.$refs.tree2.filter(val)
    }
  },
  created(){   //页面渲染后执行
    this.getAllSubjectList()
  },
  methods: {
    //获得请求数据
    getAllSubjectList() {
        subject.getSubjectList()
        .then(response => {    //.then 成功返回执行
        // console.log(response.data.list)
            this.data2 = response.data.list
        })
    },
    filterNode(value, data) {
      if (!value) return true
      return data.title.toLowerCase().indexOf(value.toLowerCase()) !== -1
    }
  }
}
</script>

